package copyengine.ui.component.scrollablePanel.element.scrollPanel
{
    import copyengine.ui.component.scrollablePanel.element.*;
    import copyengine.ui.component.scrollablePanel.scrollPanel.CEScrollPanel;

    import flash.geom.Rectangle;

    import starling.display.DisplayObject;
    import starling.display.DisplayObjectContainer;

    public class CEScrollPanelElementBasic implements ICEScrollableElement
    {
        protected var mSortRank:int;
        protected var mElementBounds:Rectangle;
        protected var mScrollPanel:CEScrollPanel;

        public function CEScrollPanelElementBasic()
        {
            calculateElementBounds();
        }

        public function dispose():void {}

        public function getBounds():Rectangle {return mElementBounds;}

        public function get sortRank():int {return mSortRank;}

        public function set sortRank(_rank:int):void {mSortRank = _rank;}

        public function show(_scrollPanelContainer:DisplayObjectContainer, _viewportRe:Rectangle):void
        {
            if (elementContainer.parent != _scrollPanelContainer) {
                _scrollPanelContainer.addChild(elementContainer);
            }
            elementContainer.visible = true;
            elementContainer.x = mElementBounds.x - _viewportRe.x;
            elementContainer.y = mElementBounds.y - _viewportRe.y;
        }

        public function hide():void
        {
            if (isRemoveFromPanelDuringHide()) {
                if (elementContainer.parent != null) {
                    elementContainer.parent.removeChild(elementContainer);
                }
            } else {
                elementContainer.visible = false;
            }
        }

        public function set scrollPanel(_scrollPanel:CEScrollPanel):void {mScrollPanel = _scrollPanel;}

        public function setInScrollPanelPos(_x:int, _y:int):void
        {
            mElementBounds.x = _x;
            mElementBounds.y = _y;
        }

        /**
         * 将Element暴露给外面,这样方便外部获得当前元素的显示对象 从而做一些特效
         * 比如: ScrollPanel刚刚加载时候然后让所有的Element飞入到应该到的位置
         */
        public function get elementContainer():DisplayObject
        {throw new Error("Child class should override this function");}


        /**
         * 当在ScrollPanel中Hide以后是否将其从渲染队列里面删除 这样可以加速渲染
         */
        protected function isRemoveFromPanelDuringHide():Boolean
        {return true;}

        /**
         * 计算当前Element的矩形信息
         */
        protected function calculateElementBounds():void
        {
            /**
             * 当元素有旋转时候,其相对于parent getBounds得到的值为旋转后的外矩形
             * 比如一个100x30的矩形进行旋转40° 其相对于parent得到的高度将大于30
             * 但是相对于自己得到的高则恒定为30
             *
             * 一个Element是否有parent 要看其是如何初始化的
             *
             * 如果将Element先放在ScrollPanel中然后用InfoTree直接Build整个ScrollPanel
             * 那么此时Element就是有parent节点的 直接用getBounds(parent)
             *
             * 如果Element是在FLA里面设置了导出类然后运行时push进去的 此时就不会有parent
             *
             */
            if (elementContainer.parent != null) {
                mElementBounds = elementContainer.getBounds(elementContainer.parent);
            } else {
                mElementBounds = elementContainer.getBounds(elementContainer);
            }
        }

    }
}
